package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.dto.InpatientAppQueryDTO;
import com.woniu.dto.RejectAdmissionDTO;
import com.woniu.entity.InpatientApplication;
import com.woniu.entity.PatientInfo;
import com.woniu.mapper.InpatientApplicationMapper;
import com.woniu.mapper.PatientInfoMapper;
import com.woniu.service.InpatientAppService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import com.woniu.vo.InpatientAppVO;
import com.woniu.websocket.NotificationServerEndpoint;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class InpatientAppServiceImpl implements InpatientAppService {
    @Resource
    private InpatientApplicationMapper mapper;
    @Override
    public ResponseUtil listInpatientApps(InpatientAppQueryDTO queryDTO) {
        // 1. 启动 PageHelper 分页
        PageHelper.startPage(queryDTO.getPageNum(), queryDTO.getPageSize());
        List<InpatientAppVO> list = mapper.listInpatientApps(queryDTO);
        PageInfo<InpatientAppVO> pageInfo = new PageInfo<>(list);
        return pageInfo.getSize() > 0 ?
                ResponseUtil.get(ResponseEnum.OK,pageInfo)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil addRecord(InpatientApplication inpatientApplication) {
        Integer result = mapper.insertSelective(inpatientApplication);
        return result > 0 ?
                ResponseUtil.get(ResponseEnum.OK,inpatientApplication.getId())
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Resource
    private PatientInfoMapper patientInfoMapper;
    @Override
    public ResponseUtil rejectAdmission(RejectAdmissionDTO dto) {
        // 1. 查找申请单
        InpatientApplication application = mapper.selectByPrimaryKey(Long.valueOf(dto.getApplicationId()));
        if (application == null) {
            throw new RuntimeException("申请单不存在");
        }

        // 2. 更新数据库状态 (假设 "3" = 拒绝)
        application.setIaStatus("3");

        // (复用您 XML 中的 updateByPrimaryKeySelective)
        mapper.updateByPrimaryKeySelective(application);

        // 3. 准备通知内容
        String patientName = "null";
        try {
            // (您需要一个 PatientInfoMapper 和 selectByPrimaryKey 方法)
            PatientInfo patient = patientInfoMapper.selectByPrimaryKey(dto.getPatientId());
            if (patient != null) {
                patientName = patient.getPatientName();
            }
        } catch (Exception e) { /* 忽略查病人姓名失败 */ }

        // 4. (关键) 构建JSON消息体
        Map<String, String> payload = new HashMap<>();
        payload.put("type", "ADMISSION_REJECTED"); // 消息类型
        payload.put("title", "入院申请被拒绝");
        payload.put("message", String.format(
                "您为病人【%s】提交的入院申请已被护士站拒绝。理由：%s",
                patientName,
                dto.getReason()
        ));

        // 5. (关键) 调用【已有的】@ServerEndpoint 类的【静态】推送方法
        NotificationServerEndpoint.sendToUser(dto.getDoctorId(), payload);

        return ResponseUtil.get(ResponseEnum.OK, "拒绝成功");
    }
}
